//
// Created by Administrator on 2020-2-12.
//

#include "DataStructre.h"
#include <stdlib.h>
#include <string.h>

bookList * New(int cap) {
    bookInfo * list = (bookInfo *)malloc(cap * sizeof(bookInfo));
    bookList * context = (bookList *)malloc(sizeof(bookList));
    context->list = list;
    context->len = 0;
    context->cap = cap;
    return context;
}

void grow(bookList * context) {
    if (context->cap >= 1024) {
        context->list = (bookInfo *)realloc(context->list, (int)(context->cap * 1.25) * sizeof(bookInfo));
        context->cap = (int)(context->cap * 1.25);
    }
    if (context->cap < 1024) {
        context->list = (bookInfo *)realloc(context->list, context->cap * 2 * sizeof(bookInfo));
        context->cap *= 2;
    }
}

void shrink(bookList * context) {
    if(context->cap <= 10){
        return;
    }

    if (context->len < context->cap / 4) {
        bookInfo *newList = (bookInfo *)realloc(context->list, (context->cap / 4) * sizeof(bookInfo));
        context->list = newList;
    }
}
